<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reportall extends CI_Controller {
	public function index(){	
		if($this->session->userdata('us') != '' && $this->session->userdata('un') != ''){
			$us = $this->session->userdata('us');
			$un = $this->session->userdata('un');
		}
		if($us == '' || $un == ''){
			$this->load->helper(array('url'));
			redirect('http://tiennhanh.com/logreport.php', 'location', 301);
		}else{
			$query = $this->db->query("SELECT * FROM sms_user WHERE user_check = '" . $us . "' and user_name= '" . $un . "'");
			if ($query->num_rows() > 0){
				$row = $query->row_array();
				if ($row['status'] == 0){					
					$result['error'] = true;
					$result['msg'] = "<b>Tài khoản chưa kích hoạt!</b><br />";
					$result['msg1'] = "Hãy soạn tin nhắn:";
					$result['smscode'] = "<b>AZ " . $un . "</b> gửi <b>8000</b>";
					$result['msg2'] = "để kích hoạt tài khoản của bạn.";
					$this->layout->setLayout('cupcake_login');
					$this->layout->setTitle('Bạn chưa kích hoạt tài khoản để sử dụng dịch vụ');
					$this->layout->view(THEME_DIR.'/website_login', $result);
				}else{
					$querychild = $this->db->query("SELECT * FROM sms_user WHERE parent_id = '" . $row['user_id_reg'] . "'");
					if ($querychild->num_rows() > 0){
						$data['uparentid'] = 1;
						$cid = "('" . $row['user_id_reg'] . "'";
						foreach ($querychild->result_array() as $rowchild){					
							$cid = $cid . ",'" . $rowchild['user_id_reg'] . "'";
						}							
						$cid = $cid . ")";
							
						if(isset($_GET['date'])){
							$arrDate = explode("-", $_GET['date']);
							$month = $arrDate[0].'-'.$arrDate[1];
						}else{
							$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
							$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
							if ($year == "") $year = date('Y');
							if ($_GET['month'] == "") $month = $year . "-" . date('m');
						}
						$arrDate = explode("-", $month);
						$data['month'] = $arrDate[1].'-'.$arrDate[0];
						//Tinh so ngay trong thang
						$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
						//===================================
						$sqlwebsite = "SELECT @row := @row + 1 AS STT, B.id_reg, U.user_name, SUM(B.mid_SMS) AS mid_SMS, SUM(B.mid_earm) AS mid_earm, SUM(B.bot_sms) as bot_sms, SUM(B.bot_earm) as bot_earm, (Sum(B.mid_earm) + Sum(B.bot_earm)) as Total
						FROM (SELECT  T.id_reg, count(T.phone) AS mid_SMS, Sum(T.sms_cost) AS mid_earm, '0' as bot_sms, '0' as bot_earm
						FROM `sms_user_mo` T
						WHERE  T.id_reg In " . $cid . " AND T.receive_date BETWEEN '" . $month . "-01 00:00:00' AND '" . $month . "-15 23:59:59'
						GROUP BY T.id_reg
						Union
						SELECT  T.id_reg, '0' AS mid_SMS, '0' AS mid_earm, count(T.phone) as bot_sms, Sum(T.sms_cost) as bot_earm
						FROM `sms_user_mo` T
						WHERE  T.id_reg In " . $cid . " AND T.receive_date BETWEEN '" . $month . "-06 00:00:00' AND '". $month . "-" . $day . " 23:59:59'
						GROUP BY T.id_reg
						) B
						INNER JOIN `sms_user` U On U.user_id_reg = B.id_reg, (Select @row := 0) R
						GROUP BY id_reg";
						$monthQuery = $this->db->query($sqlwebsite);
						if ($monthQuery->num_rows() > 0){
							$arrData = array();
							foreach ($monthQuery->result_array() as $row){					
								$data['items2'][] = $row;
								$total += intval($row['Total']);
							}
						}			
						$data['str_date'] = $strDate;		
						$data['earn_this_month'] = $total;
						//$data['month_now'] = $sql;
						
						
						$this->layout->view(THEME_DIR.'/reportall_index', $data);
					}else{
						$this->load->helper(array('url'));
						redirect('/home', 'location', 301);
					}					
				}
			}else{
				$this->load->helper(array('url'));
				redirect('http://tiennhanh.com/logreport.php', 'location', 301);
			}			
		}
	}	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */